<?php
namespace app\data\util;

use think\facade\Config;
use think\facade\Cache;
use think\facade\Db;
/**
 * http头 设置帮助类
 * @Description:
 * @ClassName: HttpUtil
 * @author xx
 * @date 2024-2-26
 */


class HttpUtil{
    /***
     * @return 输出json
     */
    public static function outJson($code=200, $message='' ,$data= [], $break = true) {
        header('Content-type: application/json');
        $arr['code'] = $code;
        $arr['message'] = $message;
        $arr['data'] =  empty($data)?NULL:$data ;
        echo json_encode($arr);
        if ($break) {
            die();
        }
    }
    /***
     * @return 输出jsonp
     */
    public static function outJsonp($code=200, $message='',$data=[], $break=true) {

        $arr['code'] = $code;
        if($arr['code'] > 0){
            $arr['message'] = $message;
        }
        $arr['data'] =  $data ;
        echo $_GET['callback'] . '(' . json_encode($arr) . ')';
        if ($break) {
            die();
        }
    }

    private static function filterNullToStr($v){
        if (is_array($v)) {
            foreach ($v as $k => $a) {
                if (is_array($a)) {
                    $v[$k] = self::filterNullToStr($v[$k]);
                } else {
                    if ($a === null) {
                        $v[$k] = '';
                    }
                    if (is_numeric($a)) {
                        $v[$k] = $a . '';
                    }
                    if (is_bool($a)) {
                        $v[$k] = $a . '';
                    }
                }
            }
        }
        return $v;
    }

    /**
     * @param string $url
     * @return mixed
     */
    public static function doGet($url)
    {
        //初始化
        $ch = curl_init();

        curl_setopt($ch, CURLOPT_URL,$url);
        // 执行后不直接打印出来
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_HEADER, false);
        // 跳过证书检查
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        // 不从证书中检查SSL加密算法是否存在
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);

        //执行并获取HTML文档内容
        $output = curl_exec($ch);

        //释放curl句柄
        curl_close($ch);

        return json_decode($output, true);
    }

    /**
     * 使用CURL方式发送get请求
     * @param string $url
     * @param array  $data
     * @return mixed
     */
    public static function doGets($url,$param)
    {
        //初始化
        $ch = curl_init();
        $url= self::urladdparam($url,$param);
        curl_setopt($ch, CURLOPT_URL,$url);
        // 执行后不直接打印出来
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_HEADER, false);
        // 跳过证书检查
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        // 不从证书中检查SSL加密算法是否存在
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);

        //执行并获取HTML文档内容
        $output = curl_exec($ch);

        //释放curl句柄
        curl_close($ch);

        return json_decode($output, true);
    }
    /**
     * @param string $url
     * @param array $post_data
     * @param array | string $token
     * @return mixed
     */
    public  static function doPost($url, $post_data, $token = '')
    {
        $ch = curl_init();

        curl_setopt($ch, CURLOPT_URL, $url);
        // 执行后不直接打印出来
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        // 设置请求方式为post
        curl_setopt($ch, CURLOPT_POST, true);
        // post的变量
        curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post_data));
        // 请求头，可以传数组
        curl_setopt($ch, CURLOPT_HEADER, false);
        if(!empty($token)){
            $header = ['Accept-Language:zh-CN','authorization:'.$token,'Content-Type:application/x-www-form-urlencoded','charset:utf-8','Accept:application/json','X-APFormat:json'];
            curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
        }
        // 跳过证书检查
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        // 不从证书中检查SSL加密算法是否存在
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);

        $output = curl_exec($ch);
        curl_close($ch);

        return json_decode($output, true);
    }

    /**
     * 使用CURL方式发送post请求
     * @param  string $url     [请求地址]
     * @param  array  $data    [array格式数据]
     * @return array
     */
    public static function postDataCurl($url, $data){

        $timeout = 5000;

        $ch = curl_init();
        curl_setopt ($ch, CURLOPT_URL, $url);
        curl_setopt ($ch, CURLOPT_POST, 1);
        curl_setopt ($ch, CURLOPT_POSTFIELDS, json_encode($data));
        curl_setopt ($ch, CURLOPT_HEADER, false);
        curl_setopt ($ch, CURLOPT_HTTPHEADER, ['Content-Type:application/json;']);
        curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, false); //处理http证书问题
        curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
        curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);

        $result = curl_exec($ch);

        if (false === $result) {
            $result =  curl_errno($ch);
        }
        curl_close($ch);
        return self::json_to_array($result) ;
    }

    /**
     * 将json字符串转化成php数组
     * @param  string $json_str
     * @return array $json_arr
     */
    public static function json_to_array($json_str){
        if(is_array($json_str) || is_object($json_str)){
            $json_str = $json_str;
        }else if(is_null(json_decode($json_str))){
            $json_str = $json_str;
        }else{
            $json_str =  strval($json_str);
            $json_str = json_decode($json_str,true);
        }
        if (!is_array($json_str)) {
            return [];
        }
        $json_arr=array();
        foreach($json_str as $k=>$w){
            if(is_object($w)){
                $json_arr[$k]= self::json_to_array($w); //判断类型是不是object
            }else if(is_array($w)){
                $json_arr[$k]= self::json_to_array($w);
            }else{
                $json_arr[$k]= $w;
            }
        }
        return $json_arr;
    }
    /**
     * 组装GET请求参数
     */
    public static function urladdparam($url,$arr=[]){
        if(empty($arr)){
            return $url;
        }
        $arr1=[];
        foreach($arr as $key=>$val){
            $arr1[]=$key."=".$val;
        }
        return rtrim($url,'/').'?'.implode('&',$arr1);
    }



    /**
     * @desc   顺丰post接口请求
     * @date   2024-03-16
     * @author luckyXiong
     */
    public static function postUrlSF($url, $data = [],$type=''){
        //获取顺丰配置
        $confDefault = $type ? 'SFTest' : 'SF';
        $sfInfo = Config::get('Logisticskey.' . $confDefault);
        $appsecret = $sfInfo['appsecret'];
        $appkey    = $sfInfo['appkey'];
        $data = (is_array($data)) ? json_encode($data, JSON_UNESCAPED_UNICODE) : $data;
        $sign = strtoupper(base64_encode(MD5(($appsecret . $data . $appsecret))));
        $headerArray = ["Content-Type:application/json;", "appKey:" . $appkey, "sign:" . $sign];
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
        curl_setopt($curl, CURLOPT_HTTPHEADER, $headerArray);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        $output = curl_exec($curl);
        curl_close($curl);
        return $output;
    }

    /**
     * @desc   金蚁post接口请求
     * @date   2024-03-16
     * @author luckyXiong
     */
    public static function postUrlJY($url, $data = []){
        //获取金蚁TOKEN
        $token    = Db::name('jinyi_token')->where('id',1)->value('token');
        $data     = (is_array($data)) ? json_encode($data, JSON_UNESCAPED_UNICODE) : $data;
        $headerArray =["Content-Type:application/json;", 'Cookie:Token=' . $token];
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
        curl_setopt($curl, CURLOPT_HTTPHEADER, $headerArray);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        $output = curl_exec($curl);
        curl_close($curl);
        return $output;
    }


    /**
     * @desc   易境通post接口请求
     * @date  2024-12-07
     * @author zhangheng
     */
    public static function postUrlYiJT($url, $data = []){
        //获取金蚁TOKEN
        $data     = (is_array($data)) ? json_encode($data, JSON_UNESCAPED_UNICODE) : $data;
        $headerArray =["Content-Type:application/json;"];
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
        curl_setopt($curl, CURLOPT_HTTPHEADER, $headerArray);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        $output = curl_exec($curl);
        curl_close($curl);
        return $output;
    }


    /**
     * @desc  TIKTOK接口请求get
     * @date  2024-03-16
     * @author luckyXiong
     */
    public static function tikGetUrl($url,$access_token=''){
        $headerArray  = ["Content-Type:application/json;", 'x-tts-access-token:' . $access_token];
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headerArray);
        $output = curl_exec($ch);
        curl_close($ch);
        return json_decode($output, true);
    }


    /**
     * @desc  TIKTOK接口请求POST
     * @date  2024-03-16
     * @author luckyXiong
     */
    public static function tikPostUrl($url, $data =[], $access_token = ''){
        $data = (is_array($data)) ? json_encode($data, JSON_UNESCAPED_UNICODE) : $data;
        $headerArray =["Content-Type:application/json;", 'x-tts-access-token:' . $access_token];
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
        curl_setopt($curl, CURLOPT_HTTPHEADER, $headerArray);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        $output = curl_exec($curl);
        curl_close($curl);
        return json_decode($output, true);
    }

    /**
     * @desc  TIKTOK接口请求POST
     * @date  2024-03-16
     * @author luckyXiong
     */
    public static function tikPutUrl($url, $data =[], $access_token = ''){
        $data = (is_array($data)) ? json_encode($data, JSON_UNESCAPED_UNICODE) : $data;
        $headerArray = ["Content-Type:application/json;", 'x-tts-access-token:' . $access_token];
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
        curl_setopt($curl, CURLOPT_HTTPHEADER, $headerArray);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PUT"); //设置请求方式
        $output = curl_exec($curl);
        curl_close($curl);
        return $output;
    }

    /**
     * @desc  17track物流商接口请求POST
     * @date  2024-03-16
     * @author luckyXiong
     */
    public static function trackPostUrl($url, $data =[]){
        $data = (is_array($data)) ? json_encode($data, JSON_UNESCAPED_UNICODE) : $data;
        $headerArray = array("Content-Type: application/json; charset=utf-8", "17token:33600DC72A94B8B45FFC0BB31469696B");
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
        curl_setopt($curl, CURLOPT_HTTPHEADER, $headerArray);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        $output = curl_exec($curl);
        curl_close($curl);
        return json_decode($output, true);
    }


    /**
     * @desc  TIKTOK广告账户get
     * @date  2024-04-15
     * @author zh
     */
    public static function ApiAdvaccountsGetUrl($url,$access_token=''){
        $headerArray = array('Access-Token:' . $access_token);
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headerArray);
        $output = curl_exec($ch);
        curl_close($ch);
        return $output;
    }

    public static function  posturlJJS($url, $data)
    {
        $headerArray = array("Content-Type:application/x-www-form-urlencoded;");
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
        curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
        curl_setopt($curl, CURLOPT_HTTPHEADER, $headerArray);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        $output = curl_exec($curl);
        curl_close($curl);
        return $output;
    }

    
    public static function posturlAirwallex($url, $id, $apikey)
    {
        $data = [];
        $headerArray = array("Content-Type:application/json;", 'x-client-id:' . $id, 'x-api-key:' . $apikey);
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
        curl_setopt($curl, CURLOPT_HTTPHEADER, $headerArray);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        $output = curl_exec($curl);
        curl_close($curl);
        return $output;
    }

    public static function geturlAirwallex($url,$token)
    {
        $headerArray = array("Content-Type:application/json;", "Authorization:Bearer ".$token);
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headerArray);
        $output = curl_exec($ch);
        curl_close($ch);
        return $output;
    }















}